package com.example.mcp.tool;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.stereotype.Component;

/**
 * 自定义计算器工具
 * 演示如何创建自定义MCP工具
 * 
 * @author Generated
 * @version 1.0.0
 */
@Component
public class CustomCalculatorTool implements FunctionCallback {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String getName() {
        return "calculator";
    }

    @Override
    public String getDescription() {
        return "执行基本数学计算，支持加法、减法、乘法、除法运算";
    }

    @Override
    public String getInputTypeSchema() {
        return """
            {
                "type": "object",
                "properties": {
                    "operation": {
                        "type": "string",
                        "description": "运算类型：add(加法), subtract(减法), multiply(乘法), divide(除法)",
                        "enum": ["add", "subtract", "multiply", "divide"]
                    },
                    "a": {
                        "type": "number",
                        "description": "第一个数字"
                    },
                    "b": {
                        "type": "number",
                        "description": "第二个数字"
                    }
                },
                "required": ["operation", "a", "b"]
            }
            """;
    }

    @Override
    public String call(String functionArguments) {
        try {
            System.out.println("计算器工具被调用，参数: " + functionArguments);
            
            // 解析JSON参数
            JsonNode args = objectMapper.readTree(functionArguments);
            
            String operation = args.get("operation").asText();
            double a = args.get("a").asDouble();
            double b = args.get("b").asDouble();
            
            double result;
            
            switch (operation.toLowerCase()) {
                case "add":
                case "加法":
                case "+":
                    result = a + b;
                    break;
                case "subtract":
                case "减法":
                case "-":
                    result = a - b;
                    break;
                case "multiply":
                case "乘法":
                case "*":
                    result = a * b;
                    break;
                case "divide":
                case "除法":
                case "/":
                    if (b == 0) {
                        return "错误：除数不能为零";
                    }
                    result = a / b;
                    break;
                default:
                    return "错误：不支持的运算操作 '" + operation + "'。支持的操作：add, subtract, multiply, divide";
            }
            
            String resultStr = String.format("计算结果：%.2f %s %.2f = %.2f", 
                a, getOperationSymbol(operation), b, result);
            
            System.out.println("计算完成: " + resultStr);
            return resultStr;
            
        } catch (Exception e) {
            String errorMsg = "计算器工具执行错误: " + e.getMessage();
            System.err.println(errorMsg);
            return errorMsg;
        }
    }

    /**
     * 获取运算符号
     */
    private String getOperationSymbol(String operation) {
        switch (operation.toLowerCase()) {
            case "add":
            case "加法":
                return "+";
            case "subtract":
            case "减法":
                return "-";
            case "multiply":
            case "乘法":
                return "×";
            case "divide":
            case "除法":
                return "÷";
            default:
                return operation;
        }
    }
}
